package com.sgcc.activiti.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.sgcc.common.core.utils.DateUtils;
import com.sgcc.common.core.utils.StringUtils;
import com.sgcc.system.api.RemoteAreaService;
import com.sgcc.system.api.domain.SysArea;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.activiti.domain.OContentAppHandle;
import com.sgcc.activiti.service.IOContentAppHandleService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 资讯处理流程Controller
 * 
 * @author sgcc
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/handle")
public class OContentAppHandleController extends BaseController
{
    @Autowired
    private IOContentAppHandleService oContentAppHandleService;

    @Autowired
    private RemoteAreaService remoteAreaService;

    /**
     * 查询资讯处理流程列表
     */
    //@PreAuthorize(hasPermi = "activiti:handle:list")
    @GetMapping("/list")
    public TableDataInfo list(OContentAppHandle oContentAppHandle)
    {
        startPage();
        List<OContentAppHandle> list = oContentAppHandleService.selectOContentAppHandleList(oContentAppHandle);
        return getDataTable(list);
    }

    /**
     * 导出资讯处理流程列表
     */
    //@PreAuthorize(hasPermi = "activiti:handle:export")
    @Log(title = "资讯处理流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OContentAppHandle oContentAppHandle) throws IOException
    {
        List<OContentAppHandle> list = oContentAppHandleService.selectOContentAppHandleList(oContentAppHandle);
        ExcelUtil<OContentAppHandle> util = new ExcelUtil<OContentAppHandle>(OContentAppHandle.class);
        util.exportExcel(response, list, "handle");
    }

    /**
     * 获取资讯处理流程详细信息
     */
    //@PreAuthorize(hasPermi = "activiti:handle:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(oContentAppHandleService.selectOContentAppHandleById(id));
    }

    /**
     * 新增资讯处理流程
     */
    //@PreAuthorize(hasPermi = "activiti:handle:add")
    @Log(title = "资讯处理流程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OContentAppHandle oContentAppHandle)
    {
        return toAjax(oContentAppHandleService.insertOContentAppHandle(oContentAppHandle));
    }

    /**
     * 修改资讯处理流程
     */
    //@PreAuthorize(hasPermi = "activiti:handle:edit")
    @Log(title = "资讯处理流程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OContentAppHandle oContentAppHandle)
    {
        return toAjax(oContentAppHandleService.updateOContentAppHandle(oContentAppHandle));
    }

    /**
     * 删除资讯处理流程
     */
    //@PreAuthorize(hasPermi = "activiti:handle:remove")
    @Log(title = "资讯处理流程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(oContentAppHandleService.deleteOContentAppHandleByIds(ids));
    }


    /**
     * 已处理记录
     */
    @ResponseBody
    @RequestMapping(value = "processedRecord", method = RequestMethod.GET)
    @ApiOperation(notes = "processedRecord", httpMethod = "GET", value = "已处理记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "procInsId", value = "工单编号(流程ID)", required = true, paramType = "query",dataType = "string")
    })
    // @ControllerLog(description = "已处理记录", module = "内容管理模块", logType = LogType.ServiceType)
    public AjaxResult processedRecord(String procInsId, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (StringUtils.isBlank(procInsId)) {
            return AjaxResult.error(201,"procInsId参数为空");
        }
        OContentAppHandle handle = new OContentAppHandle();
        handle.setAppNo(procInsId);
        List<OContentAppHandle> datas = oContentAppHandleService.processedRecord(handle);
        List<OContentAppHandle> list = datas.stream().filter(item -> {
            return !"开始".equals(item.getActName()) && !"结束".equals(item.getActName());
        } ).collect(Collectors.toList());
        for (OContentAppHandle oContentAppHandle : list) {
            //到达方式 01正常，02回退，03审核不通过
            String arriveFlag = oContentAppHandle.getArriveFlag();
            if (StringUtils.isNotBlank(arriveFlag)) {
                if ("01".equals(arriveFlag)) {
                    oContentAppHandle.setArriveFlag("正常");
                }else if ("02".equals(arriveFlag)) {
                    oContentAppHandle.setArriveFlag("回退");
                }else if ("03".equals(arriveFlag)) {
                    oContentAppHandle.setArriveFlag("审核不通过");
                }
            }
            //处理时间
            Date handleFinishTime = oContentAppHandle.getHandleFinishTime();
            if (handleFinishTime != null) {
                oContentAppHandle.setHandleFinishTimeStr(DateUtils.convertToString(handleFinishTime,DateUtils.YYYY_MM_DD_HH_MM_SS));
            }
            //处理部门
            String handleOrg = oContentAppHandle.getHandleOrg();
            List<String> orgList = Lists.newArrayList(handleOrg);
            if (StringUtils.isNotBlank(handleOrg)) {
                List<SysArea> arealist = remoteAreaService.getArealistBycode(orgList);
                if (arealist.size() > 0 && null != arealist.get(0)) {
                    oContentAppHandle.setHandleOrg(arealist.get(0).getName());
                }
            }
        }
        return AjaxResult.success(list);
    }


}
